package com.y2.authprj.utils.filter;

import java.io.IOException;
import java.io.PrintWriter;

import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import com.y2.authprj.bean.auth.model.StaffInfo;

public class LoginFilter implements Filter{

	@Override
	public void init(FilterConfig filterConfig) throws ServletException {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void doFilter(ServletRequest req, ServletResponse resp, FilterChain chain)
			throws IOException, ServletException {
		 	HttpServletRequest request = (HttpServletRequest)req;  
	        HttpServletResponse response = (HttpServletResponse)resp;  
	      
	       
	        request.setCharacterEncoding("UTF-8");  
	        response.setContentType("text/html;charset=UTF-8");  
	        HttpSession session = request.getSession();  
	        
	       
	        StaffInfo info=(StaffInfo)session.getAttribute("findByStaffName");
	        System.out.println(info+"");
	        if(request.getRequestURI().indexOf("auth/staff/login")!=-1 || info!=null){  
	            chain.doFilter(request,response);  
	        } else{  
	           
	            //out.println("<script language='javascript'>alert('你还未登录');");  
	            response.setHeader("refresh","3;/auth/staff/login");  
	            //response.sendRedirect("/pages/users/login.jsp");  
	            //request.getRequestDispatcher("/pages/users/login.jsp").forward(request,response);  
	        }  
		
	}

	@Override
	public void destroy() {
		// TODO Auto-generated method stub
		
	}

	
}
